package com.wangsanfei.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Model;
import com.wangsanfei.config.ConstConfig;

public class DicNode extends Model<DicNode>{

	private static final long serialVersionUID = 936670753294797323L;
	public static final DicNode dao = new DicNode();
	
	private static Map<String,DicNode> map=new HashMap<String,DicNode>();
	
	public List<DicNode> getDicNodes(){
		return DicNode.dao.find("select * from t_dic_node t where t.removed='0' and t.parentId=? order by t.seq",getStr("id"));
	}
	
	public DicNode getPath(String path){
		if(path!=null&&!"".equals(path)){
			if(!map.containsKey(path)){
				String[] paths=path.split(ConstConfig.DIC_DIVISION);
				DicNode dicNode=DicNode.dao.findFirst("select * from t_dic_node t where t.parentId is null and t.code=? order by t.seq",paths[0]);
				for (int i=1;i<paths.length;i++) {
					List<DicNode> dicNodes=dicNode.getDicNodes();
					for (DicNode dicNode2 : dicNodes) {
						if(paths[i].equals(dicNode2.get("code"))){
							dicNode=dicNode2;
							break;
						}else{
							dicNode=null;
						}
					}
				}
				map.put(path, dicNode);
			}
			return map.get(path);
		}
		return null;
	}
	
	public List<DicNode> getByParentDicNodes(String string) {
		return DicNode.dao.find("select name from t_dic_node t where t.removed='0' and t.parentId=? order by t.seq",string);
	}
}
